home *** CD-ROM | disk | FTP | other *** search
- unit awkworks;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ExtCtrls;
-
- type
- eAwkwardError = class(exception);
-
- TAwkwardComp = class(TCustomPanel)
- private
- fNewWindowProc : TFarProc; {pointer to our window function}
- fOldWindowProc : longint; {previous window function }
- fTargetHandle : hWnd;
- protected
- { Protected declarations }
- public
- constructor create(aOwner: tcomponent); override;
- destructor destroy; override;
- function turnOnSubClassing: boolean;
- procedure TurnOffSubClassing;
- published
- property Align;
- property Alignment;
- property BevelInner;
- property BevelOuter;
- property BevelWidth;
- property BorderWidth;
- property BorderStyle;
- property DragCursor;
- property DragMode;
- property Enabled;
- property FullRepaint;
- property Caption;
- property Color;
- property Ctl3D;
- property Font;
- property Locked;
- property ParentColor;
- property ParentCtl3D;
- property ParentFont;
- property ParentShowHint;
- property PopupMenu;
- property ShowHint;
- property TabOrder;
- property TabStop;
- property Visible;
- property OnClick;
- property OnDblClick;
- property OnDragDrop;
- property OnDragOver;
- property OnEndDrag;
- property OnEnter;
- property OnExit;
- property OnMouseDown;
- property OnMouseMove;
- property OnMouseUp;
- property OnResize;
- property OnStartDrag;
- end;
-
- function NewFormFunction(handle: hWnd; msg, wParam : Word; lParam : LongInt): LongInt; stdcall;
-
- procedure Register;
-
- implementation
-
- const cMyID : pchar = 'xyz';
-
- function NewFormFunction(handle: hWnd; msg, wParam : Word; lParam : LongInt): LongInt;
- var ThisInstance : longint;
- begin
- ThisInstance := getProp(handle, cMyID);
- if TAwkwardComp(ThisInstance).enabled
- then case msg of
- WM_LBUTTONDOWN : TAwkwardComp(ThisInstance).color := clLime;
- WM_RBUTTONDOWN : TAwkwardComp(ThisInstance).color := clWhite;
- end;
- Result := CallWindowProc(TFarProc(TAwkwardComp(ThisInstance).fOldWindowProc),
- Handle, Msg, wParam, lParam);
- end;
-
-
- constructor TAwkwardComp.create(aOwner: tcomponent);
- begin
- inherited create(aowner);
- fTargetHandle := tform(aOwner).handle;
- // note that this will only work if we do infact want to subclass the owning form, rather than the parent
- // which is fine for non-visual components
- bevelInner := bvLowered;
- bevelWidth := 3;
- caption := 'Subclassing off';
- if not TurnOnSubClassing
- then raise eawkwardError.create('Could not subclass awkard');
- end;
-
- destructor TAwkwardComp.destroy;
- begin
- turnoffSubClassing;
- inherited destroy;
- end;
-
- function TAwkwardComp.turnOnSubClassing : boolean;
- var lresult : longint;
- begin
- if SetProp(fTargetHandle, cMyID, longint(self))
- then result := true
- else begin
- result := false;
- exit;
- end;
- fOldWindowProc := GetWindowLong(fTargetHandle, GWL_WNDPROC);
- fNewWindowProc := @NewFormFunction;
- SetLastError(0); {help file suggests this}
- lResult := SetWindowLong(fTargetHandle, GWL_WNDPROC, LongInt(fNewWindowProc));
- if lResult = 0
- then result := false
- else result := true;
- caption := 'Subclassing on';
- end;
-
- procedure TAwkwardComp.TurnOffSubClassing;
- begin
- SetWindowLong(fTargetHandle, GWL_WNDPROC, longint(fOldWindowProc));
- caption := 'Subclassing off';
- color := clBtnFace;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Samples', [TAwkwardComp]);
- end;
-
- end.
-